package com.atguigu.lease.web.api.controller;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.util.JwtUtil;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.web.api.dto.UserLoginDto;
import com.atguigu.lease.web.api.interceptor.StgyApiAuthInpterceptor;
import com.atguigu.lease.web.api.service.LoginService;
import com.atguigu.lease.web.api.vo.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//请求拦截器
@RestController
@RequestMapping("/app")
@Tag(name = "会员登录模块")
public class LoginController {
    @Resource
    LoginService loginService;

    @Operation(summary = "获取短信验证码")
    @GetMapping("/login/getCode")
    public Result getCode(@RequestParam("phone")String phone){

        return Result.build(loginService.sendCode(phone));
    }
    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDto loginDto){
        // 登录成功 将用户信息创建为jwt token
        return Result.ok().data(loginService.login(loginDto));
    }
    @Operation(summary = "获取用户信息")
    @GetMapping("/info")
    public Result<UserInfoVo> info(){
        UserInfo userInfo = StgyApiAuthInpterceptor.THREAD_LOCAL.get();
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setAvatarUrl(userInfo.getAvatarUrl());
        userInfoVo.setNickname(userInfo.getNickname());
        return Result.ok(userInfoVo);
    }
}
